package com.syf.kit;


import com.syf.core.kit.SyFrameKit;
import com.syf.core.kit.resource.ResourceDelegator;
import com.syf.kit.core.dao.BaseDTO;
import com.syf.kit.core.dao.SyfRdsDaoUtil;
import com.syf.kit.core.dao.SyfRdsDaoUtilFactory;
import com.syf.kit.core.dao.mapper.SyfMapper;
import com.syf.kit.core.dao.sql.JdbcSQLProcessor;

/**
 * DFK : rds-dao提供的Kit
 */
@SyFrameKit("关系型数据库Dao对象")
public class SyfRdsDaoManager implements SyfRdsDaoUtil {
    private SyfRdsDaoUtilFactory daoUtilFactory;

    private final ResourceDelegator<SyfRdsDaoUtil> resourceDelegator;

    public SyfRdsDaoManager(SyfRdsDaoUtilFactory daoUtilFactory) {
        this.daoUtilFactory = daoUtilFactory;
        this.resourceDelegator = new ResourceDelegator<>(SyfRdsDaoManager.class.getName());
    }

    /**
     * 获取DaoUtil
     */
    private SyfRdsDaoUtil getDaoUtil() {
        if (!resourceDelegator.isDelegate()) {
            resourceDelegator.delegateResource(daoUtilFactory.buildSyfRdsDaoUtil());
        }

        return resourceDelegator.getResource();
    }

    @Override
    public JdbcSQLProcessor getSQLProcessor() {
        return getDaoUtil().getSQLProcessor();
    }

    @Override
    public <T extends BaseDTO> SyfMapper<T> getMapper(Class<T> dtoClass) {
        return getDaoUtil().getMapper(dtoClass);
    }

    @Override
    public void openConnection() {
        getDaoUtil().openConnection();
    }

    @Override
    public void close() {
        getDaoUtil().close();
    }

    @Override
    public void beginTransaction() {
        getDaoUtil().beginTransaction();
    }

    @Override
    public void closeTransaction() {
        getDaoUtil().closeTransaction();
    }

    @Override
    public void commitTransaction() {
        getDaoUtil().commitTransaction();
    }

    @Override
    public void rollbackTransaction() {
        getDaoUtil().rollbackTransaction();
    }

    @Override
    public JdbcSQLProcessor getSQLProcessor(String dsName) {
        return getDaoUtil().getSQLProcessor(dsName);
    }

    @Override
    public <T extends BaseDTO> SyfMapper<T> getMapper(String dsName, Class<T> dtoClass) {
        return getDaoUtil().getMapper(dsName, dtoClass);
    }

    @Override
    public void openConnection(String dsName) {
        getDaoUtil().openConnection(dsName);
    }

    @Override
    public void close(String dsName) {
        getDaoUtil().close(dsName);
    }

    @Override
    public void beginTransaction(String dsName) {
        getDaoUtil().beginTransaction(dsName);
    }

    @Override
    public void closeTransaction(String dsName) {
        getDaoUtil().closeTransaction(dsName);
    }

    @Override
    public void commitTransaction(String dsName) {
        getDaoUtil().commitTransaction(dsName);
    }

    @Override
    public void rollbackTransaction(String dsName) {
        getDaoUtil().rollbackTransaction(dsName);
    }

    @Override
    public void releaseResource() {
        getDaoUtil().releaseResource();
    }
}
